
import { Controller, Post, Body, SetMetadata, Request } from '@nestjs/common';
import { BaseDao } from '@/common/baseDao/base.service';
import { Util } from '@/common/util/util.service';
@SetMetadata('isLogger', true)
@Controller()
export class SystemMenuController {
    dbName: string;
    constructor(
        private baseDao: BaseDao,
        private util: Util,
    ) {
        this.dbName = 'quick-menus'
    }


    @Post('/getAll')
    async getAll(@Request() req, @Body() data) {
        let whereJson = {}
        const res = await this.baseDao.selects({
            dbName: this.dbName,
            whereJson,
        })

        let treeProps = {
            id:"menu_id",
            parent_id:"parent_id", 
            children:"children"
          };

        return {
            ...res,
            list: res['rows'],
            rows: this.util.buildTree(res['rows'],treeProps)
        }
    }

    @Post('/add')
    async add(@Body() data) {
        console.log('data', data)
        return await this.baseDao.add({
            dbName: this.dbName,
            dataJson: data
        })
    }

    @Post('/update')
    update(@Body() data) {
        console.log('data', data)
        const {
            _id,
            menu_id,
            title,
            icon,
            path,
            component,
            parent_id,
            sort,

        } = data;
        return this.baseDao.updateById({
            id: _id,
            dbName: this.dbName,
            dataJson: {
                menu_id,
                title,
                icon,
                path,
                component,
                parent_id,
                sort,
            }
        })
    }

    @Post('/delete')
    async delete(@Body() data) {
        console.log('data', data)
        return await this.baseDao.del({
            dbName: this.dbName,
            whereJson: data
        })
    }

    

}